using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Xml;

namespace CodeStream.Testing.WebAutomation
{
    /// <summary>
    /// Represents the root page of the UI Definition xml file.
    /// </summary>
    public class UIDefinitionPage
    {
        /// <summary>
        /// Creates a new instance of a UI Definition based on an XML definition.
        /// </summary>
        /// <param name="uiDefinition">The UI definition.</param>
        /// <returns>A new UIDefinitionPage object.</returns>
        public static UIDefinitionPage Create(XmlReader uiDefinition)
        {
            if (uiDefinition == null)
                throw new ArgumentNullException("uiDefinition");
            Contract.Ensures(Contract.Result<UIDefinitionPage>() != null);

            return UIDefinitionFactory.Create(uiDefinition);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIDefinitionPage"/> class.
        /// </summary>
        /// <param name="name">The name of the page.</param>
        /// <param name="containers">The containers in the entire UI Definition.</param>
        /// <param name="fields">The fields within the page.</param>
        internal UIDefinitionPage(string name, ReadOnlyCollection<UIDefinitionClass> containers, ReadOnlyCollection<UIDefinitionField> fields)
        {
            Contract.Requires(!string.IsNullOrEmpty(name));
            Contract.Requires(containers != null);
            Contract.Requires(fields != null);

            Name = name;
            Containers = containers;
            Fields = fields;
        }

        /// <summary>
        /// Gets the name of the page.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the list of all containers within the definition.
        /// </summary>
        public ReadOnlyCollection<UIDefinitionClass> Containers { get; private set; }

        /// <summary>
        /// Gets the fields that are contained within the root page.
        /// </summary>
        public ReadOnlyCollection<UIDefinitionField> Fields { get; private set; }
    }
}
